package com.leetcode.partition8;

/**
 * @author `RKC`
 * @date 2021/12/9 9:45
 */
public class LC794有效的井字游戏 {

    public static void main(String[] args) {
//        String[] board = {"XXX", "OOX", "OOX"};
//        String[] board = {"XXX", "XOO", "OO "};
//        String[] board = {"OXX", "XOX", "OXO"};
        String[] board = {"XXX", "   ", "OOO"};
        System.out.println(validTicTacToe(board));
    }

    public static boolean validTicTacToe(String[] board) {
        int count1 = 0, count2 = 0;
        boolean player1Win = false, player2Win = false;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i].charAt(j) == 'X') count1++;
                else if (board[i].charAt(j) == 'O') count2++;
            }
        }
        if (Math.abs(count1 - count2) > 1) return false;
        //行
        for (int i = 0; i < 3; i++) {
            if (board[i].charAt(0) == ' ') continue;
            boolean win = true;
            for (int j = 1; j < 3; j++) {
                if (board[i].charAt(j) != board[i].charAt(j - 1)) {
                    win = false;
                    break;
                }
            }
            if (win) {
                if (board[i].charAt(0) == 'X') player1Win = true;
                else if (board[i].charAt(0) == 'O') player2Win = true;
                if (player1Win && player2Win) return false;
            }
        }
        //列
        for (int j = 0; j < 3; j++) {
            if (board[0].charAt(j) == ' ') continue;
            boolean win = true;
            for (int i = 1; i < 3; i++) {
                if (board[i].charAt(j) != board[i - 1].charAt(j)) {
                    win = false;
                    break;
                }
            }
            if (win) {
                if (board[0].charAt(j) == 'X') player1Win = true;
                else if (board[0].charAt(j) == 'O') player2Win = true;
                if (player1Win && player2Win) return false;
            }
        }
        //两个对角线
        if (checkLeft(board)) {
            if (board[0].charAt(0) == 'X') player1Win = true;
            else if (board[0].charAt(0) == 'O') player2Win = true;
            if (player1Win && player2Win) return false;
        }
        if (checkRight(board)) {
            if (board[0].charAt(2) == 'X') player1Win = true;
            else if (board[0].charAt(2) == 'O') player2Win = true;
            if (player1Win && player2Win) return false;
        }
        if (player1Win && player2Win) return false;
        if (player1Win) return count1 == count2 + 1;
        if (player2Win) return count2 == count1;
        return count1 == count2 + 1 || count1 == count2;
    }

    private static boolean checkLeft(String[] board) {
        return board[0].charAt(0) != ' ' && board[0].charAt(0) == board[1].charAt(1) && board[0].charAt(0) == board[2].charAt(2);
    }

    private static boolean checkRight(String[] board) {
        return board[0].charAt(2) != ' ' && board[0].charAt(2) == board[1].charAt(1) && board[0].charAt(2) == board[2].charAt(0);
    }
}
